﻿using System;
using System.Collections;

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

public class BaseGame : MonoBehaviour
{
	public static BaseGame nowGame = null;

	public AudioSource music = null;


    public GameVideoUI gameVideoUI = null;

    protected GameCfgData.GameCfg.Cfg cf = null;

	protected GameCfgData.GameCfg gameCfg = null;

	protected bool hasBegin = false;

    protected string lessonId = "";

    protected bool nowMansu = true;

	protected int score = 0;
	public virtual void AddScore( bool white = false )
	{
		score++;

		// 更新计分板
		if ( gameCfg.gameType == "0")
			gameUI.GetInstance().RefreshScore(score, cf.gudian.Count, white);
		else
			gameUI.GetInstance().RefreshScore(score, cf.gudian.Count + cf.gudian2.Count, white);
	}


	public virtual void Pause()
	{
		Time.timeScale = 0;
		music.Pause();
	}

	public virtual void UnPause()
	{
		Time.timeScale = 1;
		music.UnPause ();
	}

	public virtual void FinishGame()
	{
		// 人不走了
		hasBegin = false;

		gameUI.GetInstance ().Finish (score);
	}

	public virtual void Init( string id, string lessonID = "")
	{
		gameCfg = DataManager.gameCfgData.GetCfg (id);
	}

	public virtual void ReInit( bool mansu = true)
	{
        cf = mansu ? gameCfg.mansu : gameCfg.kuaisu;
        nowMansu = mansu;
	}

	public virtual void CloseGame()
	{
		gameObject.SetActive (false);
	}

	public virtual void Clear()
	{
	}

	public virtual void Replay()
	{
	}

	public virtual void BeginGame()
	{
	}

	public virtual void ShowGuide(bool show)
	{
	}


	public virtual void PlayVideoFirst(string url)
	{
		if (string.IsNullOrEmpty (url)) {
		
			isVideoFinished = true;
			return;
		}

		string newUrl = ResLoader.Url2LocalPath (url);
		StartCoroutine(PlayStreamingVideo(newUrl));
//        gameUI.GetInstance().btnStartGame.SetActive(true);
	}

	public bool isVideoFinished = false;
	private IEnumerator PlayStreamingVideo(string url)
	{
        Handheld.PlayFullScreenMovie("file://" + url, Color.black, FullScreenMovieControlMode.CancelOnInput, FullScreenMovieScalingMode.AspectFill);
		yield return new WaitForEndOfFrame();
		yield return new WaitForEndOfFrame();

		isVideoFinished = true;
	}

    public virtual void OnBtnStartGame()
    {

    }




    

}


